import SystemUtil from 'utils/systemUtil';

export default class {
  static session = window.sessionStorage;
  static local = window.localStorage;

  //设置sessionStorage相应数据
  static setSession(key, value) {
    if (value === undefined) {
      return;
    }
    let obj = {
      data: value
    };

    this.session.setItem(key, JSON.stringify(obj));
  }

  static setLocal(key, value) {
    if (value === undefined) {
      return;
    }
    let obj = {
      data: value
    };

    this.local.setItem(key, JSON.stringify(obj));
  }

  //获取相应数据
  static getSession(key) {
    let value = this.session.getItem(key);
    if (SystemUtil.isEmpty(value)) {
      return null;
    }

    value = JSON.parse(value);
    return value.data;
  }

  static getLocal(key) {
    let value = this.local.getItem(key);
    if (SystemUtil.isEmpty(value)) {
      return null;
    }

    value = JSON.parse(value);
    return value.data;
  }

  //清除相应sessionStorage里的值，key不传为清空sessionStorage里所有值
  static removeSession(key) {
    if (key === undefined) {
      this.session.clear();
    } else {
      this.session.removeItem(key);
    }
  }

  static removeLocal(key) {
    if (key === undefined) {
      this.local.clear();
    } else {
      this.local.removeItem(key);
    }
  }

  static removeAll() {
    this.removeLocal();
    this.removeSession();
  }
};
